package com.example.question.tree.tree3;

import com.example.question.tree.tree1.TreeNode;

public class Code15_129 {

    public int sumNumbers(TreeNode root) {
        return f(root, 0);
    }

    private int f(TreeNode node, Integer num) {
        // 当前是叶子的时候就要结束
        if (node.left == null && node.right == null) {
            return 10 * num + node.val;
        }
        int left = 0;
        int right = 0;
        if (node.left != null) {
            left = f(node.left, 10 * num + node.val);
        }
        if (node.right != null) {
            right = f(node.right, 10 * num + node.val);
        }
        return left + right;
    }
}
